package com.cmict.data.bpm.service.message;

import com.cmict.data.framework.web.config.WebProperties;
import com.cmict.data.bpm.controller.admin.task.vo.task.message.TaskMessageCreateReqVO;
import com.cmict.data.bpm.convert.message.BpmMessageConvert;
import com.cmict.data.bpm.convert.message.TaskMessageConvert;
import com.cmict.data.bpm.dal.dataobject.task.TaskMessageDO;
import com.cmict.data.bpm.dal.mysql.task.TaskMessageMapper;
import com.cmict.data.bpm.enums.message.BpmMessageEnum;
import com.cmict.data.bpm.service.message.dto.BpmMessageSendWhenProcessInstanceApproveReqDTO;
import com.cmict.data.bpm.service.message.dto.BpmMessageSendWhenProcessInstanceRejectReqDTO;
import com.cmict.data.bpm.service.message.dto.BpmMessageSendWhenTaskCreatedReqDTO;
import com.cmict.data.system.api.sms.SmsSendApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * BPM 消息 Service 实现类
 *
 * @author awesome
 */
@Service
@Validated
@Slf4j
public class BpmMessageServiceImpl implements BpmMessageService {

    @Resource
    private SmsSendApi smsSendApi;
    @Resource
    private WebProperties webProperties;

    @Autowired
    private TaskMessageMapper taskMessageMapper;

    @Override
    public void sendMessageWhenProcessInstanceApprove(BpmMessageSendWhenProcessInstanceApproveReqDTO reqDTO) {
        Map<String, Object> templateParams = new HashMap<>();
        templateParams.put("processInstanceName", reqDTO.getProcessInstanceName());
        templateParams.put("detailUrl", getProcessInstanceDetailUrl(reqDTO.getProcessInstanceId()));
        smsSendApi.sendSingleSmsToAdmin(BpmMessageConvert.INSTANCE.convert(reqDTO.getStartUserId(),
                BpmMessageEnum.PROCESS_INSTANCE_APPROVE.getSmsTemplateCode(), templateParams));
    }

    @Override
    public void sendMessageWhenProcessInstanceReject(BpmMessageSendWhenProcessInstanceRejectReqDTO reqDTO) {
        Map<String, Object> templateParams = new HashMap<>();
        templateParams.put("processInstanceName", reqDTO.getProcessInstanceName());
        templateParams.put("reason", reqDTO.getReason());
        templateParams.put("detailUrl", getProcessInstanceDetailUrl(reqDTO.getProcessInstanceId()));
        smsSendApi.sendSingleSmsToAdmin(BpmMessageConvert.INSTANCE.convert(reqDTO.getStartUserId(),
                BpmMessageEnum.PROCESS_INSTANCE_REJECT.getSmsTemplateCode(), templateParams));
    }

    @Override
    public void sendMessageWhenTaskAssigned(BpmMessageSendWhenTaskCreatedReqDTO reqDTO) {
        Map<String, Object> templateParams = new HashMap<>();
        templateParams.put("processInstanceName", reqDTO.getProcessInstanceName());
        templateParams.put("taskName", reqDTO.getTaskName());
        templateParams.put("startUserNickname", reqDTO.getStartUserNickname());
        templateParams.put("detailUrl", getProcessInstanceDetailUrl(reqDTO.getProcessInstanceId()));
        smsSendApi.sendSingleSmsToAdmin(BpmMessageConvert.INSTANCE.convert(reqDTO.getStartUserId(),
                BpmMessageEnum.TASK_ASSIGNED.getSmsTemplateCode(), templateParams));
    }

    private String getProcessInstanceDetailUrl(String taskId) {
        return webProperties.getAdminUi().getUrl() + "/bpm/process-instance/detail?id=" + taskId;
    }

    @Override
    public Long createTaskMessage(TaskMessageCreateReqVO createReqVO) {
        // 插入
        TaskMessageDO taskMessage = TaskMessageConvert.INSTANCE.convert(createReqVO);
        //:设置抄送消息的模板ID,用于给用户发送消息提醒，如站内信，邮件或短信当前不发消息，设置一个固定值
        taskMessage.setTemplateId(9999L);
        taskMessageMapper.insert(taskMessage);
        // 返回
        return taskMessage.getId();
    }

}
